package com.oneday.ashop.webgate.book;


import com.oneday.ashop.core.entity.CommonConstant;
import com.oneday.ashop.core.entity.Page;
import com.oneday.ashop.core.entity.User;
import com.oneday.ashop.core.entity.book.Blog;
import com.oneday.ashop.core.entity.book.Book;
import com.oneday.ashop.core.entity.book.Comment;
import com.oneday.ashop.core.entity.book.Mood;
import com.oneday.ashop.webgate.base.ftp.SFtpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.List;

/**
 * 读书控制层
 * User:bob.yang
 * Date 2015/8/14 0014
 */
@Controller
@RequestMapping("book")
public class BookControl {

    @Autowired
    private BookService bookService;

    /**
     * 获得图书主页面
     * @return
     */
    @RequestMapping("book_menu")
    public ModelAndView bookMenu() {
        // 搜索推荐书籍
        List<Book> recBookList = bookService.queryRecommendBooks();
        // 搜索热门书籍
        List<Book> hotBookList = bookService.queryHotBooks();
        // 搜索热门评论
        List<Comment> commentList = bookService.queryHotComments();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName( "book/book_menu" );
        modelAndView.addObject("recBookList", recBookList);
        modelAndView.addObject("hotBookList", hotBookList);
        modelAndView.addObject("commentList", commentList);
        return modelAndView;
    }

    /**
     * 上传图书
     * @param
     * @return
     */
    @RequestMapping("upload")
    public String upload(String bookName, String bookAuthor, String bookPublishDate,
                         MultipartFile bookImage, String picName, String strImage, MultipartFile bookFile,
                         MultipartFile image, String bookIntroduction,HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        // 文件上传
        int result = bookService.upload(bookName, bookAuthor, bookPublishDate,
                bookImage, picName, strImage, bookFile, bookIntroduction, user);
        model.addAttribute("result", result);
        return "book/book_callback";
    }

    /**
     * 上传封面
     * @param icon
     * @param model
     * @return
     */
    @RequestMapping("upload_img")
    public String upLoadImg(MultipartFile icon, Model model) {
        String url = bookService.uploadImg(icon);
        model.addAttribute("imgUrl", url);
        return "book/book_image_cut";
    }

    /**
     * 截取封面
     * @param imgUrl
     * @param x
     * @param y
     * @param width
     * @param height
     * @return
     */
    @RequestMapping("crop_img")
    public String cropImg(String imgUrl, int width, int height, int x, int y) {
        bookService.cropImg(imgUrl, x, y, width, height);
        return "book/book_image_cut";
    }

    @RequestMapping("test")
    public void testJson(Model model) {
        model.addAttribute("test", "请不要再让我伤心！");
    }

    /**
     * 我的圈子
     * @return
     */
    @RequestMapping("read_circle")
    public String readCircle(HttpServletRequest request, Model model, Page<Mood> page) {
        User user = (User)request.getSession().getAttribute("user");
        //查找心情
        bookService.findMoodPage(user.getId(), page);
        model.addAttribute("page", page);
        return "book/read_circle";
    }
//    @RequestMapping("get_upload")
//    public String uplod() {
//        return
//    }

    /**
     * 发表心情
     * @param content
     * @param model
     * @return
     */
    @RequestMapping("publish_mood")
    public String publishMood(@RequestParam("content") String content, HttpServletRequest request, String imgUrls, Page<Mood> page, Model model) {
        User user = (User)request.getSession().getAttribute("user");
        //如果用户为空 返回到登录界面
        if(user == null) {
            return "user/login";
        }
        bookService.publishMood(user.getId(), imgUrls, content);
        //查找心情
        bookService.findMoodPage(user.getId(), page);
        model.addAttribute("page", page);

        return "book/read_circle";
    }

    /**
     * 心情回应
     * @param commentContent
     */
    @RequestMapping("mood_comment")
    public void reply(@RequestParam("commentContent") String commentContent, Integer mid, HttpServletRequest request) {
        User user = (User)request.getSession().getAttribute("user");
        bookService.moodComment(user.getId(), mid, commentContent);
    }

    /**
     * 点赞
     * @param mid
     */
    @RequestMapping("mood_praise")
    public void moodPraise(@RequestParam("mid")Integer mid, HttpServletRequest request) {
        User user = (User)request.getSession().getAttribute("user");
        bookService.moodPraise(user.getId(), mid);
    }

    /**
     * 删除
     * @param mid
     * @param request
     */
    @RequestMapping("mood_delete")
    public void moodDelete(@RequestParam("mid")Integer mid, HttpServletRequest request) {
        User user = (User)request.getSession().getAttribute("user");
        bookService.moodDelete(user.getId(), mid);
    }

    /**
     * 获得日志编辑页面
     * @return
     */
    @RequestMapping("get_blog_edit")
    public String getBlogEdit() {
        return "book/book_write_blog";
    }

    /**
     * 写日志
     * @param request
     * @param title 日志标题
     * @param content 日志内容
     * @param blogPrivacy 可见范围
     * @param couldReply 是否可以回复
     * @param model
     * @return
     */
    @RequestMapping("write_blog")
    public String writeBlog(HttpServletRequest request, String title, String content, Integer blogPrivacy, Integer couldReply, Model model) {
        User user = (User)request.getSession().getAttribute("user");
        bookService.writeBlog(user.getId(), title, content, blogPrivacy, couldReply);
        //查找心情
        Page page = new Page();
        bookService.findMoodPage(user.getId(), page);
        model.addAttribute("page", page);
        return "book/read_circle";
    }


    /**
     * 日志
     * @param bid
     * @param model
     * @return
     */
    @RequestMapping("blog_detail")
    public String blogDetail(Integer bid, Model model) {
        Blog blog = bookService.findBlog(bid);
        model.addAttribute("blog", blog);
        return "book/book_blog";
    }

    /**
     * 上传文件通用接口
     * @param file
     * @param model
     * @return
     */
    @RequestMapping("update_img")
    public void updateImg(MultipartFile file, Model model) {
        String url = bookService.uploadImg(file);
        model.addAttribute("imgUrl", url);
    }
}
